package com.infore.maintenance.main.config;

import cn.hutool.core.date.DateUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.infore.common.model.entity.TBaseQuerySql;
import com.infore.maintenance.main.service.TBaseQuerySqlService;
import com.infore.maintenance.main.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PostConstruct;
import java.util.Date;
import java.util.List;


/**
 * 项目启动初始化数据
 */
@Configuration
@Slf4j
public class DataInitializationConfig {

    @Autowired
    private TBaseQuerySqlService TBaseQuerySqlService;

    @Autowired
    private RedisUtil redisUtil;

    /**
    * @Deacription
    * 由JDK提供了@PostConstruct注解，主要用于在Spring容器启动时执行某些操作或者任务，@PostConstruct注解一般放在BEAN的方法上，
    * 一旦BEAN初始化完成之后，将会调用这个方法。
    * @MethodName init
    * @Author lijianhua
    * @Date 2022/12/17 11:31
    * @最后修改人: lijianhua
    * @最后修改时间：2022/12/17 11:31
    * @Version 4.0
    * @param
    * @return
    **/
    @PostConstruct
    public void init() {
        // 项目启动初始化基本数据
        log.info("数据初始化开始: " + DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
        Wrapper<TBaseQuerySql> wrapper = new EntityWrapper<TBaseQuerySql>();
        List<TBaseQuerySql> list = TBaseQuerySqlService.selectList(wrapper);
        for (TBaseQuerySql vo:list ) {
            redisUtil.set(vo.getSqlCode(), JSONUtil.toJsonStr(vo) );
        }
        log.info("数据初始化结束: " + DateUtil.format(new Date(),"yyyy-MM-dd HH:mm:ss"));
    }
}
